﻿using System;
using System.Collections.Generic;
using UnityEngine;

namespace YummyGame.Framework
{
    public class SkillAction_chgvar : SkillAction
    {
        private string var_name;
        private Expression value;
        private string change_type;
        public override void Dispose()
        {
            var_name = string.Empty;
            value = null;
            change_type = string.Empty;
        }

        public override ActionRunState Excute()
        {
            var v = value.Calculate();
            if(change_type == Skill_Change_Value_Type.OWNER)
            {
                if (!Owner.HasVariable(var_name))
                    Debug.LogError("未定义的变量:" + var_name);
                else
                    SetVariable(var_name, value.Calculate());
            }
            else
            {
                if (!Executer.Runtime.HasVariable(var_name))
                    Debug.LogError("未定义的变量:" + var_name);
                else
                    SetVariable(var_name, value.Calculate());
            }
            
            return ActionRunState.Success;
        }

        public override void Reactive()
        {
            SkillActionPrototype_chgvar chgvar = Prototype as SkillActionPrototype_chgvar;
            var_name = chgvar.var_name;
            //value = chgvar.value;
            value = ParseRuntimeExpression(chgvar.value);
            change_type = chgvar.change_type;
        }

        public override ActionRunState Run()
        {
            return ActionRunState.Success;
        }
    }
}
